package com.ken.controller.user;

import com.ken.constant.JwtClaimsConstant;
import com.ken.dto.UserLoginDTO;
import com.ken.entity.User;
import com.ken.properties.JwtProperties;
import com.ken.result.Result;
import com.ken.service.UserService;
import com.ken.utils.JwtUtil;
import com.ken.vo.UserLoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/user/user")
@Slf4j
@Api(tags ="C端-用户接口")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private JwtProperties jwtProperties;

    @PostMapping("/login")
    @ApiOperation("登录")
    public Result<UserLoginVO> login(@RequestBody UserLoginDTO login){
        log.info("微信用户登录，授权码为：{}",login.getCode());
        User user = userService.wxLogin(login);

        Map claims = new HashMap();
        claims.put(JwtClaimsConstant.USER_ID,user.getId());
        String token = JwtUtil.createJWT(jwtProperties.getUserSecretKey(), jwtProperties.getUserTtl(), claims);
        UserLoginVO build = UserLoginVO.builder()
                .id(user.getId())
                .openid(user.getOpenid())
                .token(token)
                .build();
        return Result.success(build);
    }
}
